﻿package tools;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
	public static void main(String[] args) {
		Calendar cdr1 = new GregorianCalendar();
		//月份是从0开始的,0表示1月
		Calendar cdr2 = new GregorianCalendar(2008, 0, 1);
		Calendar cdr3 = Calendar.getInstance();
		cdr3.set(2012, 11, 11, 11, 11, 11);
		System.out.println(cdr3.getTime());
		//2008-1-1至今的毫秒数
		System.out.println(cdr1.getTime().getTime() - cdr2.getTime().getTime());
		//某天当前月份的最后一天
		cdr2.set(Calendar.YEAR, 2008);
		cdr2.set(Calendar.MONTH, 1);//1表示二月份
		System.out.println(cdr2.getActualMaximum(Calendar.DATE));
		//将当天设置成当月最后一天
		cdr2.set(Calendar.DAY_OF_MONTH,
				cdr2.getActualMaximum(Calendar.DAY_OF_MONTH));
		System.out.println(dateFormat(cdr2));
		System.out.println("Year: "+Calendar.getInstance().get(Calendar.YEAR));
		//Month是从0开始的
		System.out.println("Month: "+Calendar.getInstance().get(Calendar.MONTH));
		System.out.println("Date: "+Calendar.getInstance().get(Calendar.DATE));
		System.out.println("Hour: "+Calendar.getInstance().get(Calendar.HOUR));
		System.out.println("Minute: "+Calendar.getInstance().get(Calendar.MINUTE));
		System.out.println("Second: "+Calendar.getInstance().get(Calendar.SECOND));

	}

	public static String dateFormat(Calendar date) {
		return new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());
	}

}
